package cn.stylefeng.guns.modular.sht.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.sht.entity.Question;
import cn.stylefeng.guns.modular.sht.mapper.QuestionMapper;
import cn.stylefeng.guns.modular.sht.model.params.QuestionParam;
import cn.stylefeng.guns.modular.sht.model.result.QuestionResult;
import cn.stylefeng.guns.modular.sht.service.QuestionService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 考题信息 服务实现类
 * </p>
 *
 * @author
 * @since 2021-01-22
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements QuestionService {

    @Override
    public void add(QuestionParam param) {
        Question entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(QuestionParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(QuestionParam param) {
        Question oldEntity = getOldEntity(param);
        Question newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public QuestionResult findBySpec(QuestionParam param) {
        return null;
    }

    @Override
    public List<QuestionResult> findListBySpec(QuestionParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(QuestionParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public int cntTotal(QuestionParam param) {
        QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<Question>().eq("question_bank_id", param.getQuestionBankId());
        int count = this.baseMapper.selectCount(questionQueryWrapper);
        return count;
    }

    private Serializable getKey(QuestionParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Question getOldEntity(QuestionParam param) {
        return this.getById(getKey(param));
    }

    private Question getEntity(QuestionParam param) {
        Question entity = new Question();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
